﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Autenticacao
{
    public partial class frm_padmincriar : Form
    {
        System.Data.SqlClient.SqlConnection con;
        public frm_padmincriar()
        {
            InitializeComponent();
        }

        private void frm_padmincriar_Load(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label1_Click_1(object sender, EventArgs e)
        {

        }

        private void cmdcriaconta_Click(object sender, EventArgs e)
        {
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = @"Data Source = ANDRÉ-PC\SQLEXPRESS; Initial Catalog = horsepw_bd; Integrated Security = True;";
            con.Open();

            try
            {
                //Validação de dados antes de inserir na BD
                if (txtnumlicenca.TextLength < 6)
                {
                    MessageBox.Show("Número da licença Incorreto!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
                else if (txtuser.Text == "" || txtplvchave.Text == "" || txtnome.Text == "" || txtnumlicenca.Text == "" || txtdtnasc.Text == "")
                    MessageBox.Show("Preencha todos os campos!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                else
                    if ((string.Compare(txtuser.Text, "administrador", true) == 0) || (string.Compare(txtuser.Text, "admin", true) == 0) || (string.Compare(txtuser.Text, "administrator", true) == 0))
                    {
                        MessageBox.Show("Utilizador não permitido!", "Erro", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                        {
                            SqlCommand command1 = con.CreateCommand();                           
                            command1.CommandText = "Insert into Utilizador (nome, nro_licenca, data_nascimento, Login, Pass) Values ('" + txtnome.Text + "', '" + txtnumlicenca.Text + "', '" + txtdtnasc.Text + "', '" + txtuser.Text + "', '" + txtplvchave.Text + "')";
                            command1.CommandType = CommandType.Text;

                            SqlDataReader reader1 = command1.ExecuteReader();
                            
                            MessageBox.Show("Utilizador registado com sucesso!", "Registo Efetuado", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            lblinfo_suc.Visible = true;
                        }
            }
            catch (Exception er)
            {
                MessageBox.Show("Dado Inválido!", "Erro!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                con.Close();
            }

            con.Close();
        }

        private void cmdvoltar_Click(object sender, EventArgs e)
        {
            this.Close();
            frm_padmin frm = new frm_padmin();
            frm.Show();
        }
    }
}
